using System.Reflection;
using System.Text.Json;
using Microsoft.AspNetCore.OData;
using Microsoft.AspNetCore.OData.Query.Expressions;
using Microsoft.EntityFrameworkCore;
using Microsoft.OData.Edm;
using OData.MinimalApi.Configuration;
using OData.MinimalApi.Extensions;
using OData.Data;

var builder = WebApplication.CreateBuilder(args);
IEdmModel model = EdmModelBuilder.GetEdmModel();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseInMemoryDatabase("ODataDb"));
builder.Services.AddSwaggerGen();
builder.Services.ConfigureHttpJsonOptions(options => {
	options.SerializerOptions.WriteIndented = true;
	options.SerializerOptions.IncludeFields = true;
});

builder.Services.AddOData(q => q.EnableAll());
ODataMiniMetadata me = new();
me.Services = services =>
{
	services.AddSingleton<IFilterBinder, FilterBinder>();
};
builder.Services.AddEndpoints(Assembly.GetExecutingAssembly());

var app = builder.Build();
app.MakeSureDbCreated();
app.MapODataServiceDocument("$document", model);
app.MapODataMetadata("$metadata", model);
app.MapOdataEndpoints(model);

if (app.Environment.IsDevelopment())
{
	app.UseODataRouteDebug();
	app.UseSwagger();
	app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.Run();

